package com.bill.gaty.mfidelity.dao.impl;

 
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

 
import com.bill.gaty.mfidelity.beans.Gtf;

import com.bill.gaty.mfidelity.dao.GtfDao;
import com.bill.gaty.mfidelity.dto.GtfDate;
import com.bill.gaty.mfidelity.util.DaoAbstract;
import com.bill.gaty.mfidelity.util.GtfConstantes;

public class GtfDaoImpl extends DaoAbstract implements GtfDao {

	public GtfDaoImpl(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	protected List<Gtf> cursorToGtf(Cursor c) {
		// TODO Auto-generated method stub
		if (c==null||c.getCount() == 0)
			return null;
 List<Gtf> gtfs=new ArrayList<Gtf>();
		  
 Gtf gtf = null;
		 while(c.moveToNext()){
			 
			 gtf = new   Gtf();
			 gtf.setLastUpdate((new GtfDate(c.getLong(GtfConstantes.NUM_COL_GTF_LAST_UPDATE))));
			 gtf.setSavedate((new GtfDate(c.getLong(GtfConstantes.NUM_COL_GTF_SAVE_DATE))));
			 gtf.setJsonData(c.getString(GtfConstantes.NUM_COL_GTF_JSONDATA));
			 gtf.setVotreNom(c.getString(GtfConstantes.NUM_COL_GTF_VOTRE_NOM));
			 gtf.setNomDeVotreAmi(c.getString(GtfConstantes.NUM_COL_GTF_LE_NOM_DE_VOTRE_AMI));
			 gtf.setReponse (c.getString(GtfConstantes.NUM_COL_GTF_REPONSE));
					
			 gtf.setId((c.getLong(GtfConstantes.NUM_COL_GTF_ID)));
			 
			 gtfs.add(gtf);
			 
		 }
		c.close();
 
		 
		return gtfs;
	}
	@Override
	public long add(Gtf gtf) {
		// TODO Auto-generated method stub
		
		ContentValues values = new ContentValues();
	 	values.put(GtfConstantes.COL_GTF_JSONDATA,gtf.getJsonData());
	 	values.put(GtfConstantes.COL_GTF_LAST_UPDATE,gtf.getLastUpdate().getTime());
	 	values.put(GtfConstantes.COL_GTF_SAVE_DATE,gtf.getSavedate().getTime());
	 	values.put(GtfConstantes.COL_GTF_VOTRE_NOM,gtf.getVotreNom());
	 	values.put(GtfConstantes.COL_GTF_LE_NOM_DE_VOTRE_AMI,gtf.getNomDeVotreAmi());
	 	values.put(GtfConstantes.COL_GTF_REPONSE,gtf.getReponse());
	 	   
	 	 
	
	 long id= getBdd().insert(GtfConstantes.TABLE_GTF, null, values);
	 gtf.setId(id);
	 return id;
	}
	@Override
	public long update(Gtf gtf) {
		// TODO Auto-generated method stub
		ContentValues values = new ContentValues();
	 	values.put(GtfConstantes.COL_GTF_JSONDATA,gtf.getJsonData());
	 	values.put(GtfConstantes.COL_GTF_LAST_UPDATE,gtf.getLastUpdate().getTime());
	 	values.put(GtfConstantes.COL_GTF_SAVE_DATE,gtf.getSavedate().getTime());
	 	values.put(GtfConstantes.COL_GTF_ID,gtf.getId());
	 	values.put(GtfConstantes.COL_GTF_VOTRE_NOM,gtf.getVotreNom());
	 	values.put(GtfConstantes.COL_GTF_LE_NOM_DE_VOTRE_AMI,gtf.getNomDeVotreAmi());
	 	values.put(GtfConstantes.COL_GTF_REPONSE,gtf.getReponse());
	 	
		return getBdd().update(GtfConstantes.TABLE_GTF, values, GtfConstantes.COL_GTF_ID + " = " +gtf.getId(), null);  
		 
	}
	@Override
	public long remove(long gtfid) {
		// TODO Auto-generated method stub
		return getBdd().delete(GtfConstantes.TABLE_GTF, GtfConstantes.COL_GTF_ID + " = " +gtfid, null);  
		
	}
	@Override
	public List<Gtf> findAll() {
		// TODO Auto-generated method stub
		Cursor c = getBdd().query(GtfConstantes.TABLE_GTF, GtfConstantes.COLS_GTF, null, null, null, null, GtfConstantes.COL_GTF_SAVE_DATE+" DESC");
		return   cursorToGtf (c);
		
	}
	@Override
	public Gtf find(long gtfid) {
		// TODO Auto-generated method stub
		Cursor c = getBdd().query(GtfConstantes.TABLE_GTF, GtfConstantes.COLS_GTF,GtfConstantes.COL_GTF_ID+"="+gtfid, null, null, null, null);
		List<Gtf> l = cursorToGtf(c);
		if(null!=l&&l.size()>0)return l.get(0);
		return null;
	}
	 
	 
}
